/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;
import java.util.Arrays;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.auction.service.mapper.ProblemMapper;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.auction.domain.Problem;
import co.yixiang.modules.auction.service.ProblemService;
import co.yixiang.modules.auction.service.dto.ProblemQueryCriteria;
import co.yixiang.modules.auction.service.dto.ProblemDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author yhh
* @date 2021-02-20
*/
@AllArgsConstructor
@Api(tags = "常见问题管理")
@RestController
@RequestMapping("/api/Problem")
public class ProblemController {

    private final ProblemService ProblemService;
    private final ProblemMapper problemMapper;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','Problem:list')")
    public void download(HttpServletResponse response, ProblemQueryCriteria criteria) throws IOException {
        ProblemService.download(generator.convert(ProblemService.queryAll(criteria), ProblemDto.class), response);
    }

    @GetMapping
    @Log("查询常见问题")
    @ApiOperation("查询常见问题")
    @PreAuthorize("@el.check('admin','Problem:list')")
    public ResponseEntity<Object> getProblems(ProblemQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ProblemService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增常见问题")
    @ApiOperation("新增常见问题")
    @PreAuthorize("@el.check('admin','Problem:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Problem resources){
        return new ResponseEntity<>(ProblemService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改常见问题")
    @ApiOperation("修改常见问题")
    @PreAuthorize("@el.check('admin','Problem:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Problem resources){
        ProblemService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除常见问题")
    @ApiOperation("删除常见问题")
    @PreAuthorize("@el.check('admin','Problem:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            ProblemService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取新人教程")
    @ApiOperation("获取新人教程")
    @GetMapping("/getPeopleDetail")
    public ResponseEntity getPeopleDetail() {
        return new ResponseEntity<>(problemMapper.getPeopleDetail() , HttpStatus.OK);
    }


    @Log("保存新人教程")
    @ApiOperation("保存新人教程")
    @GetMapping("/savePeople")
    public ResponseEntity savePeople(@RequestParam(value = "url") String url) {
        problemMapper.savePeople(url);
        return new ResponseEntity<>( HttpStatus.OK);
    }

}
